#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "bmp.h"
#include <QMessageBox>
#include <QInputDialog>
#include <QFileDialog>
#include <QFile>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    ui->stackedWidget->setCurrentIndex(0);
}

MainWindow::~MainWindow()
{
    delete ui;
}

//全局变量
bool imageLoaded = false;
QFile *image;
Bmp *bmp;
int zoom_level = 0;          //缩小一次减一，放大一次加一

//图片显示
void MainWindow::display(Bmp *displaybmp)
{
    QImage *displayImage = displaybmp->toQImage();
    ui->displayArea->clear();
    ui->displayArea->setPixmap(QPixmap::fromImage(*displayImage));
}


//图片加载
void MainWindow::on_ReadImage_clicked()
{
    //read image
    QString fileLocation = QFileDialog::getOpenFileName(this, "导入图片", "/", "Image Files(*.bmp)");
    if(fileLocation.length() == 0)
    {
        return;
    }
    QFile aimage(fileLocation);
    if(!aimage.open(QIODevice::ReadWrite))
    {
        if (QMessageBox::question(this, "警告" ,"图片未导入，请检查路径后重新导入图片！") == QMessageBox::Yes)
        {
            on_ReadImage_clicked();
        }
        else
        {
            return;
        }
    }
    else
    {
        //图片加载与显示
        bmp = new Bmp(&aimage);
        display(bmp);
    }

    zoom_level = 0;
    imageLoaded = true;
    image = &aimage;
}


//判断是否加载图片并处理
void MainWindow::checkImageLoaded()
{
    while (!imageLoaded)
    {
        if (QMessageBox::question(this, "警告" ,"图片未导入，请导入图片！") == QMessageBox::Yes)
        {
            on_ReadImage_clicked();
        }
        else
        {
            return;
        }
    }
}


//灰度图转换
void MainWindow::on_GrayScale_clicked()
{
    zoom_level = 0;
    checkImageLoaded();
    //判断是否加载图片并处理
    if (imageLoaded)
    {
        //灰度图转换
        bmp->toGray();
        display(bmp);
    }
    else
    {
        return;
    }
}


//高斯模糊
void MainWindow::on_Gausssian_blur_clicked()
{
    zoom_level = 0;
    //判断是否加载图片并处理
    checkImageLoaded();

    //高斯模糊
    if(imageLoaded)
    {
        bmp->Gausssian_blur();
        display(bmp);
    }
    else
    {
        return;
    }
}

void MainWindow::on_exit_clicked()
{
    if (QMessageBox::question(this, "退出", "是否退出？请确保图片修改已保存。") == QMessageBox::Yes)
    {
        QApplication::exit(0);
    }
}

void MainWindow::on_saveImage_clicked()
{
    QString saveLocation = QFileDialog::getSaveFileName(this, "保存图片", "/", "Image Files(*.bmp)");
    if (saveLocation.length() == 0)
    {
        return;
    }
    if (!bmp->save(saveLocation))
    {
        if (QMessageBox::question(this, "保存失败", "保存失败，请检查路径或文件名。") == QMessageBox::Yes)
        {
            on_saveImage_clicked();
        }
        else
        {
            return;
        }
    }
    else
    {
        QMessageBox::information(this, "保存成功", "图片已保存。");
    }
}


void MainWindow::on_Up_to_Dowm_clicked()
{
    zoom_level = 0;
    //判断是否加载图片并处理
    checkImageLoaded();

    //上下翻转
    if(imageLoaded)
    {
        bmp->Up_to_Down();
        display(bmp);
    }
    else
    {
        return;
    }
}


void MainWindow::on_right_to_left_clicked()
{
    zoom_level = 0;
    //判断是否加载图片并处理
    checkImageLoaded();

    //左右翻转
    if(imageLoaded)
    {
        bmp->right_to_left();
        display(bmp);
    }
    else
    {
        return;
    }
}


void MainWindow::on_spin_90_clicked()
{
    zoom_level = 0;
    //判断是否加载图片并处理
    checkImageLoaded();

    //旋转90度
    if(imageLoaded)
    {
        bmp->spin_90();
        display(bmp);
    }
    else
    {
        return;
    }
}

void MainWindow::on_zoom_in_clicked()
{
    //判断是否加载图片并处理
    checkImageLoaded();

    zoom_level++;

    if (zoom_level == 1)
    {
        if (QMessageBox::question(this, "放大", "图片已是原大小，继续放大将显示不全，是否继续放大？") == QMessageBox::No)
        {
            zoom_level--;
            return;
        }
        else
        {
            display_zoomed();
        }
    }

    else
    {
        display_zoomed();
    }
}


void MainWindow::on_zoom_out_clicked()
{
    //判断是否加载图片并处理
    checkImageLoaded();

    zoom_level--;
    display_zoomed();
}

void MainWindow::display_zoomed()
{
    Bmp zoomed_bmp(*bmp);
    zoomed_bmp.zoom(zoom_level);
    display(&zoomed_bmp);
}

int slider_up_now = 0;
int slider_down_now = 0;
int slider_left_now = 0;
int slider_right_now = 0;

void MainWindow::display_for_cut(Bmp *displaybmp = bmp,
                                 int slider_up = slider_up_now, int slider_down = slider_down_now,
                                 int slider_left = slider_left_now, int slider_right = slider_right_now)
{
    ui->horizontalSlider_up->setMaximum(100);
    ui->horizontalSlider_down->setMaximum(100);
    ui->verticalSlider_left->setMaximum(100);
    ui->verticalSlider_right->setMaximum(100);

    Bmp *temp_bmp_for_cut = new Bmp(*displaybmp);
    temp_bmp_for_cut->add_line_for_cut(slider_up, slider_down, slider_left, slider_right);

    QImage *displayImage = temp_bmp_for_cut->toQImage();
    ui->displayArea_for_cut->clear();
    ui->displayArea_for_cut->setPixmap(QPixmap::fromImage(*displayImage));
}

void MainWindow::on_cut_clicked()
{
    if (!imageLoaded)
    {
        QMessageBox::information(this, "图片未导入", "图片未导入，请先导入图片！");
        return;
    }
    ui->stackedWidget->setCurrentIndex(1);
    display_for_cut(bmp);
}


void MainWindow::on_cancel_cut_clicked()
{
    slider_up_now = 0;
    slider_down_now = 0;
    slider_left_now = 0;
    slider_right_now = 0;
    ui->horizontalSlider_up->setValue(slider_up_now);
    ui->horizontalSlider_down->setValue(slider_down_now);
    ui->verticalSlider_left->setValue(slider_left_now);
    ui->verticalSlider_right->setValue(slider_right_now);
    ui->stackedWidget->setCurrentIndex(0);
}


void MainWindow::on_ok_cut_clicked()
{

    long new_Width = slider_up_now - slider_down_now;
    new_Width *= (slider_up_now - slider_down_now > 0 ? 1 : -1);
    long new_Height = slider_left_now - slider_right_now;
    new_Height *= (slider_left_now - slider_right_now > 0 ? 1 : -1);

    if (new_Width < 3 || new_Height < 3)
    {
        QMessageBox::information(this, "区域过小", "选择区域过小，请重新选择！");
        return;
    }

    Bmp *cuted_bmp = new Bmp(*bmp);
    cuted_bmp->cut(slider_up_now, slider_down_now, slider_left_now, slider_right_now);

    on_cancel_cut_clicked();
    display(cuted_bmp);
    bmp = cuted_bmp;
}


void MainWindow::on_horizontalSlider_up_valueChanged(int value)
{
    slider_up_now = value;
    display_for_cut();
}


void MainWindow::on_horizontalSlider_down_valueChanged(int value)
{
    slider_down_now = value;
    display_for_cut();
}


void MainWindow::on_verticalSlider_left_valueChanged(int value)
{
    slider_left_now = value;
    display_for_cut();
}


void MainWindow::on_verticalSlider_right_valueChanged(int value)
{
    slider_right_now = value;
    display_for_cut();
}

